/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.panomedic.kernel.controllers;

import com.panomedic.Constants;
import com.panomedic.colors.Pixel;
import com.panomedic.utils.Utils;
import com.panomedic.colors.ColorConstants;
import com.panomedic.colors.YxyPixel;
import com.panomedic.gui.PhotoPanel;
import com.panomedic.kernel.VisualHandler;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.prefs.Preferences;

/**
 *
 * @author Yare
 */
class PhotoPanelListener extends MouseAdapter {

    private PhotoPanel photoPanel;

    public PhotoPanelListener(PhotoPanel photoPanel) {
        this.photoPanel = photoPanel;
    }
    
    @Override
    public void mouseClicked(MouseEvent e){
        
        Preferences prefs = Preferences.userRoot().node(Constants.defNodePath); 
        
        BufferedImage image = photoPanel.getImage();
        Dimension orig = null;
        String status = new String();
        if (image != null) {
                        
            
            Dimension cell = new Dimension(photoPanel.getWidth(), photoPanel.getHeight());
            Dimension dim = Utils.getScaledSize(cell, image.getWidth(),image.getHeight());
            Dimension img = new Dimension(image.getWidth(),image.getHeight());
            int startX = (cell.width - dim.width)/2;
            int startY = (cell.height - dim.height)/2;
            int origX = (int) ((int) (e.getX() - startX) * ((double) img.width / dim.width));
            int origY = (int) ((int) (e.getY() - startY) * ((double) img.height / dim.height));
            orig = new Dimension(origX, origY);
                    
            
            
            if (e.getX() < startX || e.getX() > (cell.width - startX) || 
                    e.getY() < startY || e.getY() > (cell.height - startY)) {
//                VisualHandler.getInstance().getFrame().setStatus("Position: Out of image.");
                status += "Position: Out of image.";
            } else {
//                VisualHandler.getInstance().getFrame().setStatus("Position: [" + orig.width + "," + orig.height + "]");
                status += "Position: [" + orig.width + "," + orig.height + "]";
                int px = 0;
                px = image.getRGB(origX, origY);
                Color c = new Color(px);
                Pixel px2 = Pixel.getPixel(prefs.getInt(Constants.CS_TYPE, ColorConstants.HSV), new int[]{c.getRed(),c.getGreen(),c.getBlue(),c.getAlpha()});
                
                if (px != 0 && c.getAlpha() == 255) status += " - Color(RGB): ["+
                        c.getRed()+","+
                        c.getGreen()+","+
                        c.getBlue()+"] - " + px2;
                        
                
            }
        } else {
            status += "No image loaded.";
        }
        
        VisualHandler.getInstance().getFrame().setStatus(status);
            
           
            
        
    }
}
